<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *客户列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'销售员体验率'),
);
if($_SESSION['role_id']>8 && $_SESSION['role_id']<15){
	exi('您无权访问！','error');
}
//客户列表
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$return['params']=array();
	$params=array('role'=>6,'status'=>1,'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:30;
	$list=pdo_getslice('jy_yxw_worker',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		if($_RQ['createtime']){
			$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
			$time_range=json_decode($time_range,true);
			$between=array($time_range['tstart'],$time_range['tend']);
			$return['params']['createtime']=$time_range;
		}
		$status4=array(4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,32,33,34,35,36,37,38,39,40,41,42,100);
		$tingnumparams=$ting2numparams=$xuenumparams=$baonumparams=$tuibaonumparams=$memberparams=array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
		$tuibaonumparams['status']=array(101,102,103);
		if(!$between){
			$memberparams['createtime >']=0;
			$ting2numparams['ting2_time >']=0;
			$tingnumparams['ting_time >']=0;
			$xuenumparams['sxue_time >']=0;
			$baonumparams['bao_time >']=0;
			$tuibaonumparams['createtime >']=0;
		}else{
			$memberparams['createtime between']=$between;
			$ting2numparams['ting2_time between']=$between;
			$tingnumparams['ting_time between']=$between;
			$xuenumparams['sxue_time between']=$between;
			$baonumparams['bao_time between']=$between;
			$tuibaonumparams['createtime between']=$between;
		}
		foreach($list as &$item){
			$ting2numparams['worker6id']=$tingnumparams['worker6id']=$xuenumparams['worker6id']=$baonumparams['worker6id']=$tuibaonumparams['worker6id']=$memberparams['worker6id']=$item['id'];
			//$item['member']=$this->model('worker')->member_show($item['id']);
			$item['tingnum']=pdo_count('jy_yxw_member',$tingnumparams);
			$item['ting2num']=pdo_count('jy_yxw_member',$ting2numparams);
			$item['xuenum']=pdo_count('jy_yxw_member',$xuenumparams);
			$item['baonum']=pdo_count('jy_yxw_member',$baonumparams);
			$item['tuinum']=pdo_count('jy_yxw_member',$tuibaonumparams);
			if($item['tingnum']>0){
				$item['ting2rate']=price($item['ting2num']*100/$item['tingnum']);
			}
			if($item['tingnum']>0){
				$item['xue1rate']=price($item['xuenum']*100/$item['tingnum']);
			}
			if($item['ting2num']>0){
				$item['xue2rate']=price($item['xuenum']*100/$item['ting2num']);
			}
			if($item['tingnum']>0){
				$item['bao1rate']=price($item['baonum']*100/$item['tingnum']);
			}
		}
	}
	$heji=array('title'=>'合计');
	foreach($list as $v){
		$heji['tingnum'] +=$v['tingnum'];
		$heji['ting2num'] +=$v['ting2num'];
		$heji['xuenum'] +=$v['xuenum'];
		$heji['baonum'] +=$v['baonum'];
		$heji['tuinum'] +=$v['tuinum'];
	}
	if($heji['tingnum']>0){
		$heji['ting2rate']=price($heji['ting2num']*100/$heji['tingnum']);
	}
	if($heji['tingnum']>0){
		$heji['xue1rate']=price($heji['xuenum']*100/$heji['tingnum']);
	}
	if($heji['ting2num']>0){
		$heji['xue2rate']=price($heji['xuenum']*100/$heji['ting2num']);
	}
	if($heji['tingnum']>0){
		$heji['bao1rate']=price($heji['baonum']*100/$heji['tingnum']);
	}
	$list[]=$heji;
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	//页面显示设置
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'export'=>'export',
		'list'=>array('data'=>'list','del_one'=>false,'delall'=>false,'edit'=>false,'list'=>array(
			array('name'=>'title','title'=>'销售员','type'=>'string'),
			array('name'=>'tingnum','title'=>'体验','type'=>'string'),
			array('name'=>'ting2num','title'=>'体用','type'=>'string'),
			array('name'=>'xuenum','title'=>'进班','type'=>'string'),
			array('name'=>'baonum','title'=>'成交','type'=>'string'),
			array('name'=>'tuinum','title'=>'退成交','type'=>'string'),
			array('name'=>'ting2rate','title'=>'体验体用率','type'=>'unitstring','unit'=>'%'),
			array('name'=>'xue1rate','title'=>'体验进班率','type'=>'unitstring','unit'=>'%'),
			array('name'=>'bao1rate','title'=>'体验成交率','type'=>'unitstring','unit'=>'%'),
		)),
		'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'销售员名称','name'=>'title','help'=>''),
			array('name'=>'createtime','width'=>15,'title'=>'时间段','type'=>'time_range')
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='export'){
	//客户列表
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$return['params']=array();
	$params=array('role'=>6,'status'=>1,'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:10;
	$list=pdo_getslice('jy_yxw_worker',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		if($_RQ['createtime']){
			$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
			$time_range=json_decode($time_range,true);
			$between=array($time_range['tstart'],$time_range['tend']);
			$return['params']['createtime']=$time_range;
		}
		$status4=array(4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,32,33,34,35,36,37,38,39,40,41,42,100);
		$tingnumparams=$ting2numparams=$xuenumparams=$baonumparams=$tuibaonumparams=$memberparams=array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
		$tuibaonumparams['status']=array(101,102,103);
		if(!$between){
			$memberparams['createtime >']=0;
			$ting2numparams['ting2_time >']=0;
			$tingnumparams['ting_time >']=0;
			$xuenumparams['sxue_time >']=0;
			$baonumparams['bao_time >']=0;
			$tuibaonumparams['createtime >']=0;
		}else{
			$memberparams['createtime between']=$between;
			$ting2numparams['ting2_time between']=$between;
			$tingnumparams['ting_time between']=$between;
			$xuenumparams['sxue_time between']=$between;
			$baonumparams['bao_time between']=$between;
			$tuibaonumparams['createtime between']=$between;
		}
		foreach($list as &$item){
			$ting2numparams['worker6id']=$tingnumparams['worker6id']=$xuenumparams['worker6id']=$baonumparams['worker6id']=$tuibaonumparams['worker6id']=$memberparams['worker6id']=$item['id'];
			//$item['member']=$this->model('worker')->member_show($item['id']);
			$item['tingnum']=pdo_count('jy_yxw_member',$tingnumparams);
			$item['ting2num']=pdo_count('jy_yxw_member',$ting2numparams);
			$item['xuenum']=pdo_count('jy_yxw_member',$xuenumparams);
			$item['baonum']=pdo_count('jy_yxw_member',$baonumparams);
			$item['tuinum']=pdo_count('jy_yxw_member',$tuibaonumparams);
			if($item['tingnum']>0){
				$item['ting2rate']=price($item['ting2num']*100/$item['tingnum']);
			}
			if($item['tingnum']>0){
				$item['xue1rate']=price($item['xuenum']*100/$item['tingnum']);
			}
			if($item['ting2num']>0){
				$item['xue2rate']=price($item['xuenum']*100/$item['ting2num']);
			}
			if($item['tingnum']>0){
				$item['bao1rate']=price($item['baonum']*100/$item['tingnum']);
			}
		}
	}
	$heji=array('title'=>'合计');
	foreach($list as $v){
		$heji['tingnum'] +=$v['tingnum'];
		$heji['tingnum2'] +=$v['tingnum2'];
		$heji['xuenum'] +=$v['xuenum'];
		$heji['baonum'] +=$v['baonum'];
		$heji['tuinum'] +=$v['tuinum'];
	}
	if($heji['tingnum']>0){
		$heji['ting2rate']=price($heji['ting2num']*100/$heji['tingnum']);
	}
	if($heji['tingnum']>0){
		$heji['xue1rate']=price($heji['xuenum']*100/$heji['tingnum']);
	}
	if($heji['ting2num']>0){
		$heji['xue2rate']=price($heji['xuenum']*100/$heji['ting2num']);
	}
	if($heji['tingnum']>0){
		$heji['bao1rate']=price($heji['baonum']*100/$heji['tingnum']);
	}
	$list[]=$heji;
	$return['list']=$list;
	$header=array(
		'title'=>'员工名称',
		'tingnum'=>'体验',
		'ting2num'=>'体用',
		'xuenum'=>'试用',
		'baonum'=>'成交',
		'tuinum'=>'退成交',
		'ting2rate'=>'体验体用率',
		'xue1rate'=>'体验试用率',
		'xue2rate'=>'体用试用率',
		'bao1rate'=>'体验成交率',
	);
	model('core/tools')->export2excel($header,$list,'销售员数据');
}